package code.c08;

import java.util.*;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/10/20 11:27
 * @package code.c08
 * @description HashSet --> TreeSet 按书名比较
 */

public class Library {
    Map<String, Book> books;

    public Library() {
        books = new HashMap<String, Book>();
    }

    /**
     * 添加操作
     *
     * @param book
     * @return
     */
    public boolean addBook(String book_id, Book book) {
        if (books.containsKey(book_id)) {//如果数据已经存在，则不能添加📕
            return false;
        } else {
            books.put(book_id, book);
            return true;
        }
    }

    /**
     * 删除操作
     *
     * @param book_id
     * @return
     */
    public boolean removeBook(String book_id) {
        return books.remove(book_id) != null;
    }

    /**
     * 获取个数
     *
     * @return
     */
    public int getTotalNumberOfBooks() {
        return books.size();
    }

    /**
     * 获取指定的书
     *
     * @param book_id
     * @return
     */
    public Book getBook(String book_id) {
        if (books.isEmpty()) {
            return null;
        } else {
            return books.get(book_id);
        }
    }

    /**
     * 获取所有的书本
     *
     * @return 返回book 的集合
     */
    public Set<Book> getAllBooks() {
        TreeSet<Book> bookTreeSet = new TreeSet<>(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                // 先比较书名
                if (!o1.getTitle().equals(o2.getTitle())) {
                    return o1.getTitle().compareTo(o2.getTitle());
                }
                // 再比较作者
                if (!o1.getAuthor().equals(o2.getAuthor())) {
                    return o1.getAuthor().compareTo(o2.getAuthor());
                }
                // 最后比较isbn
                return o1.getIsbn().compareTo(o2.getIsbn());
            }
        });

        Set<String> strings = books.keySet();

        // 遍历
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) {
            bookTreeSet.add(books.get(iterator.next()));
        }

        return bookTreeSet;
    }
}

/**
 * book 类
 */
class Book {
    private String isbn;
    private String title;
    private String author;

    public Book(String isbn, String title, String author) {
        this.isbn = isbn;
        this.title = title;
        this.author = author;
    }

    public String getIsbn() {
        return isbn;
    }


    public String getTitle() {
        return title;
    }


    public String getAuthor() {
        return author;
    }


    @Override
    public String toString() {
        return "Book{" +
                "isbn='" + isbn + '\'' +
                ", title='" + title + '\'' +
                ", author='" + author + '\'' +
                '}';
    }
}

/**
 * 测试类
 */
class TestBook {
    public static void main(String[] args) {
        Library library = new Library();

        Book book1 = new Book("192-21-21", "张三", "a罗翔");
        Book book2 = new Book("192-21-51", "张三", "b罗翔");
        Book book3 = new Book("152-21-21", "机房", "大罗翔");
        Book book4 = new Book("192-21-91", "张三", "a罗翔");
        Book book5 = new Book("192-21-31", "张三普法", "小罗翔");

        System.out.println("======= 添加书本");
        System.out.println(library.addBook("1001", book1));
        System.out.println(library.addBook("1002", book2));
        System.out.println(library.addBook("1003", book3));
        System.out.println(library.addBook("1004", book4));
        System.out.println(library.addBook("1005", book5));

        System.out.println("======= 书本个数");
        System.out.println(library.getTotalNumberOfBooks());

        System.out.println("======= 删除书本");
//        System.out.println(library.removeBook("1001"));

        System.out.println("======= 获取单个书本");
        System.out.println(library.getBook("1002"));

        System.out.println("======= 获取全部书本");
        System.out.println(library.getAllBooks());
    }
}